# -*- coding: utf-8 -*-
# @Time : 2021/9/9 19:33
# @Author : hello
# @Email : xxx@gmail.com
# @File : CRM_ST_KHGL_057.py
# @Project : crm

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

"""线索模块-验证编辑功能-所有信息合法输入-部分输入，部分二次输入"""
driver=webdriver.Chrome()
try:

    # driver.get("http://192.168.1.155/crm")
    # driver.implicitly_wait(30)
    # driver.maximize_window()
    #
    # name="admin"
    # password="admin123"
    # username_locator=(By.NAME,'name')
    # password_locator=(By.NAME,'password')
    # login_locator=(By.NAME,'submit')
    #
    # driver.find_element(*username_locator).send_keys(name)
    # driver.find_element(*password_locator).send_keys(password)
    # driver.find_element(*login_locator).click()


    driver.implicitly_wait(30)
    driver.maximize_window()
    """前置条件-已登录crm系统且进入线索展示界面"""
    cookies={'name':'PHPSESSID','value':'5o422r3lhfr2q9gmj0mmbfl2v7'}
    driver.get("http://192.168.1.155")
    driver.add_cookie(cookies)
    driver.get("http://192.168.1.155/crm/index.php?m=leads")


    clickadd=(By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')
    sleep(2)
    driver.find_element(*clickadd).click()

    fzrs="admin"
    companys="郑州科技有限公司"
    sources="合作伙伴"
    lxrs="张三"
    zws="业务经理"
    phones="15632455632"
    zcs="先生"
    yxs="xx@xx.com"
    xs="河南省"
    ss="郑州市"
    qs="惠济区"
    lxtimes="2021-09-17"
    lxnrs="初步沟通"
    bzs="小型软件公司"
    jds="东大街"

    fzr=(By.ID,'owner_name')
    company=(By.ID,'name')
    source=(By.ID,'source')
    lxr=(By.ID,'contacts_name')
    zw=(By.ID,'position')
    phone=(By.ID,'mobile')
    zc=(By.ID,'saltname')
    yx=(By.ID,'email')
    adress_x=(By.NAME,"address['state']")
    adress_s=(By.NAME,"address['city']")
    adress_q=(By.NAME,"address['area']")
    jd = (By.NAME, "address['street']")
    lxtimek=(By.ID,"nextstep_time")
    lxnr=(By.ID,'nextstep')
    bz=(By.ID,'description')
    bc=(By.CSS_SELECTOR,'input[value="保存"]')

    driver.find_element(*fzr).clear()
    driver.find_element(*fzr).send_keys(fzrs)
    driver.find_element(*company).send_keys(companys)
    driver.find_element(*lxr).send_keys(lxrs)
    driver.find_element(*zw).send_keys(zws)
    driver.find_element(*phone).send_keys(phones)
    driver.find_element(*yx).send_keys(yxs)
    driver.find_element(*lxtimek).send_keys(lxtimes)
    driver.find_element(*lxnr).send_keys(lxnrs)

    driver.find_element(*bz).send_keys(bzs)
    sleep(3)
    driver.find_element(*bc).click()
    sleep(3)
#进入编辑页面
    tables=(By.CSS_SELECTOR,"#form1 > table")
    tbodys=(By.TAG_NAME,"tbody")
    trs = (By.TAG_NAME, "tr")
    tds = (By.TAG_NAME, "td")
    a = (By.TAG_NAME, "a")
    table=driver.find_element(*tables)
    tbody=table.find_element(*tbodys)
    trss=tbody.find_elements(*trs)
    for tr in trss:
        td=tr.find_elements(*tds)
        if td[2]:
            td[-1].find_elements(*a)[-1].click()
            sleep(5)
            sel_locator=driver.find_element(*source)
            Select(sel_locator).select_by_visible_text(sources)
            zc_locator=driver.find_element(*zc)
            Select(zc_locator).select_by_visible_text(zcs)
            x_locator = driver.find_element(*adress_x)
            Select(x_locator).select_by_visible_text(xs)
            s_locator = driver.find_element(*adress_s)
            Select(s_locator).select_by_visible_text(ss)
            q_locator = driver.find_element(*adress_q)
            Select(q_locator).select_by_visible_text(qs)
            driver.find_element(*jd).send_keys(jds)
            sleep(3)
            driver.find_element(*bc).click()
            break
except Exception as e:
    raise e
finally:
    sleep(3)
    driver.quit()
